(in-package :lilu-dmenu-example)

(defun test-dmenu-number (&optional (n 20))
  (let ((seqs (loop for i from 0 to n
                    collect (list i (format nil "~r" i)))))
    (multiple-value-bind (sel match-p)
        (dmenu "number? " seqs :item-fn #'car)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))
    (multiple-value-bind (sel match-p)
        (dmenu "number? " seqs :item-fn #'cadr)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))))

(defun test-rofi-number (&optional (n 20))
  (let ((seqs (loop for i from 0 to n
                    collect (list i (format nil "~r" i)))))
    (multiple-value-bind (sel match-p)
        (rofi "number? " seqs :item-fn #'car)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))
    (multiple-value-bind (sel match-p)
        (rofi "number? " seqs :item-fn #'cadr :only-match-p t)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))))

(defun test-dynamic-menu-number (&optional (n 20))
  (let ((seqs (loop for i from 0 to n
                    collect (list i (format nil "~r" i)))))
    (multiple-value-bind (sel match-p)
        (dynamic-menu "number? " seqs :item-fn #'car)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))
    (multiple-value-bind (sel match-p)
        (dynamic-menu "number? " seqs :item-fn #'cadr)
      (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))))


(defun test-rofi-number-with-themes (&optional (n 20))
  (let ((seqs (loop for i from 0 to n
                    collect (list i (format nil "~r" i)))))
    (flet ((test ()
             (format *debug-io* "~A~%" (assoc :fg *rofi-theme*))
             (multiple-value-bind (sel match-p)
                 (rofi "number? " seqs :item-fn #'car)
               (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))))
      (test)
      (let ((*rofi-theme* '()))
        (test))
      (let ((my-theme '((:font . "DejaVu 15") (:lines . "4") (:theme . "DarkBlue"))))
        (let ((*rofi-theme* (append my-theme *rofi-theme*)))
          (test))))))

(defun test-dmenu-number-with-themes (&optional (n 20))
  (let ((seqs (loop for i from 0 to n
                    collect (list i (format nil "~r" i)))))
    (flet ((test ()
             (multiple-value-bind (sel match-p)
                 (dmenu "number? " seqs :item-fn #'car)
               (format t "Selection: ~A (~A) ~%" sel (if match-p "matched" "unmatched")))))
      (test)
      (let ((*dmenu-theme* '()))
        (test))
      (let ((my-theme '((:bg . "#ff0033") (:lines . "3") (:fg . "#0011aa"))))
        (let ((*dmenu-theme* (append my-theme *dmenu-theme*)))
          (test))))))


(defun test-select-a-files (&optional (dir "~/"))
  (let ((current-dir dir))
    (flet ((gen-seqs (d)
             (append
              '("..")
              (uiop/filesystem:subdirectories d)
              (uiop/filesystem:directory-files d)))
           (get-action (sel match-p)
             (if match-p
                 (if (uiop:directory-pathname-p sel)
                     (values :go sel)
                     (if (equal sel "..")
                         (values :go (uiop/pathname:pathname-parent-directory-pathname current-dir))
                         (values :select sel)))
                 (values :create sel))))
      (do ((act :go)
           (act-val dir)
           (i 0 (+ 1 i)))
          ((or (> i 10) (not (eql act :go))) (when act-val
                                               (values act-val act)))
        (setf (values act act-val)
              (multiple-value-call #'get-action
                (dynamic-menu (format nil "~A> file? " (pathname current-dir))
                              (gen-seqs current-dir) :item-fn #'pathname)))
        (when (eql act :go)
          (setf current-dir act-val))))))
